********************************************************************************
** 	TITLE: f12_cost_benefit.do

**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table XII: Social Cost Effects
********************************************************************************
**	Note: Includes Cost of Crime and Cost of Days in Jail
********************************************************************************
set sortseed 13


use "$output_data/estimate.dta", clear
preserve
***************************************************************************
***** Panel A: Cost of Crime
***************************************************************************
*******Complier control means for cost of crime by months 3, 6, 9, 12. 
//Code taken from "$code/c_complier_control_mean.do"
cap drop stay_day
cap drop less_than_3
gen stay_day = stay_rec*30
gen less_than_3 = stay_day<=3

	foreach y in crime_costs_3m crime_costs_6m crime_costs_9m crime_costs_12m {
		cap gen `y'_less_than_3 = `y'*less_than_3
		ivreghdfe  `y'_less_than_3 ( less_than_3 = i.D_remove_any) ///
			 if  `y'>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control)
		global ccm_`y' : di %9.3f _b[less_than_3]
	}
restore 

	est clear
	//stay ignite_0 for recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
				
*************************************************************
*Regressions
*************************************************************
			foreach v of varlist crime_costs_*m  {
						local controls $auxiliary_control						
						
						ivreghdfe `v' (ignite_0 stay = interact_proxy_any D_remove_any) `controls' D_ignite_proxy ///
							if `v'>=0, ///
							cluster(inmate) ffirst a($design_control)
						eststo tsls_ce`v'1any
						estadd scalar clust e(N_clust): tsls_ce`v'1any
						mat SWF=e(first)
						local temp_f1 = SWF[8,1]
						local temp_f2 = SWF[8,2]
						local f1: display %8s `temp_f1'
						local f2: display %8s `temp_f2'
						estadd local f1 "`f1'" : tsls_ce`v'1any
						estadd local f2 "`f2'" : tsls_ce`v'1any
						sum `v' if e(sample)==1 & D_ignite_proxy==0&D_remove_any==0
						local meantemp = r(mean)
						local mean: display %8s `meantemp'
						estadd local meancont "${ccm_`v'}" : tsls_ce`v'1any
						local fstat = string(e(rkf), "%8s")
						estadd local fstat_2 "`fstat'": tsls_ce`v'1any
						
						local lin_p  =r(p)
						
						
						if `lin_p'<0.01 {
							local star1="***"
						}
						if `lin_p'>=0.01 & `lin_p'<0.05 {
							local star1="**"
						}
						if `lin_p'>=0.05 & `lin_p'<0.1 {
							local star1="*"
						}
						if `lin_p'>=0.1 {
							local star1=""
						}
			}
*************************************************************
* Make Table
*************************************************************
			estout  ///
				tsls_cecrime_costs_3m1any tsls_cecrime_costs_6m1any ///
				tsls_cecrime_costs_9m1any tsls_cecrime_costs_12m1any ///
				using "$tables/2sls_costs.tex", style(tex)  ///
				label nobaselevels collabels(none) mlabels(none) /// 
				keep(ignite_0) ///
				varlabel(ignite_0 "Months in IGNITE") ///
				cells(b(star fmt(%8.2f)) se(par fmt(%8.2f))) /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				stats(foo meancont N, ///
				fmt(0 %9.3f %9.0gc) /// 
				labels("\hspace{0.5cm}" "Control Complier Mean" ///
					"Observations"))	replace
	
	
***************************************************************************
***** Panel B: Cost of Incarceration
***************************************************************************
**************Cost of Days in Jail
 use "$output_data/sag_gen_updated.dta", clear

//Jail Days
	//start by converting the time to recidivate in Genesee County or Saginaw County return_time_sg into days
cap drop timegap
gen timegap = return_time_sg*30

keep sg_id booking_date release_date timegap return_3m return_6m return_9m return_12m
format %td booking_date
sort sg_id booking_date
egen ID = group(sg_id booking_date)

forvalues j=3(3)12 {
	gen ref_date_`j'm = release_date + `j'*30
	format  %td ref_date_`j'm
}

bysort sg_id (booking_date): gen inmate_booking_id = _n

preserve
		keep sg_id ID booking_date release_date timegap
		bysort sg_id: gen total_num_obs = _N
		expand  total_num_obs
		bysort ID: gen inmate_booking_id = _n
		rename booking_date booking_date_x
		rename release_date release_date_x
		rename timegap timegap_x
		keep sg_id inmate_booking_id booking_date_x release_date_x timegap_x
		tempfile duplicates_booking_date
		save `duplicates_booking_date'
restore

merge 1:m sg_id inmate_booking_id  using `duplicates_booking_date', nogen keep(3)

keep sg_id booking_date release_date ref_date_3m ref_date_6m ref_date_9m ref_date_12m ///
booking_date_x release_date_x timegap_x timegap return_3m return_6m return_9m return_12m
order sg_id booking_date release_date ref_date_3m ref_date_6m ref_date_9m ref_date_12m ///
booking_date_x release_date_x timegap_x timegap
sort sg_id ref_date_3m

gen out_jail_date = release_date_x + timegap_x
format  %td out_jail_date

forvalues j=3(3)12 {
	bysort sg_id booking_date: egen time_out_jail_`j'm = total(timegap_x) if (release_date<out_jail_date & out_jail_date<ref_date_`j'm) 
}

egen return_total=rowtotal(return_3m return_6m return_9m return_12m)

forvalues j=3(3)12 {
//gen the code here	
	gen back_jail_`j'm = .
//shifting the data gen	
	replace back_jail_`j'm = 0 if return_`j'm==0
	replace back_jail_`j'm = -9 if return_`j'm==-9
	replace back_jail_`j'm = -10 if return_`j'm==-10  // 2 missings for all cases
	
//Additional conditions
	replace back_jail_`j'm = 0 if (ref_date_`j'm<out_jail_date & back_jail_`j'm == .)
	replace back_jail_`j'm = (`j'*30 - time_out_jail_`j'm) if return_`j'm==1 & 	back_jail_`j'm == .
	replace back_jail_`j'm = 0 if (out_jail_date < release_date & back_jail_`j'm == .)
	replace back_jail_`j'm = `j'*30 if (return_total==4 & timegap_x==0 & back_jail_`j'm == .)
	replace back_jail_`j'm = 1 if timegap_x==`j'*30 & return_`j'm==1
}

collapse (max) back_jail_3m back_jail_6m back_jail_9m back_jail_12m,by(sg_id booking_date release_date)

tempfile back_jail_date
save `back_jail_date'


use "$output_data/estimate.dta", clear
merge 1:1 sg_id booking_date release_date using `back_jail_date', nogen keep(3) assert(2 3)

			//stay ignite_0 for Recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec

preserve				
*******Complier control means for cost of crime by months 3, 6, 9, 12. 

cap drop stay_day
cap drop less_than_3
gen stay_day = stay*30
gen less_than_3 = stay_day<=3
gen year_booked = year(booking_date)
	
	foreach y in back_jail_3m back_jail_6m back_jail_9m back_jail_12m {
		cap gen `y'_less_than_3 = `y'*less_than_3
		ivreghdfe  `y'_less_than_3 ( less_than_3 = i.D_remove_any) ///
			 if  `y'>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control)
		global ccm_`y' : di %9.3f _b[less_than_3]*70
	}

*******************************************************************************
* Load Data
********************************************************************************
restore 

*************************************************************
*Regressions
*************************************************************
	
	gen year_booked = year(booking_date)
	gen interact_div_year_month = cat_division*year_booked*month_booked

	est clear

    foreach v in 3m 6m 9m 12m { 
		replace back_jail_`v' = -999 if return_`v'< 0
	}
	
    foreach v in back_jail_3m back_jail_6m back_jail_9m back_jail_12m { 
           
                local controls $auxiliary_control
				
            * IV                
            ivreghdfe `v' (ignite_0 stay = interact_proxy_any D_remove_any) `controls' D_ignite_proxy /// 
                if `v'>=0, /// 
                cluster(inmate) ffirst a($design_control)
            eststo tsls`v'1any
            estadd scalar clust e(N_clust): tsls`v'1any
			mat SWF=e(first)
			local temp_f1 = SWF[8,1]
			local temp_f2 = SWF[8,2]
			local f1: display %9.3f `temp_f1'
			local f2: display %9.3f `temp_f2'
			estadd local f1 "`f1'" : tsls`v'1any
			estadd local f2 "`f2'" : tsls`v'1any
			sum `v' if e(sample)==1 & D_ignite_proxy==0&D_remove_any==0
			local meantemp = r(mean)
			local mean: display %9.3f `meantemp'
			estadd local meancont "${ccm_`v'}" : tsls`v'1any
			local fstat = string(e(rkf), "%8s")
			estadd local fstat_2 "`fstat'": tsls`v'1any
            
            * Multiply coefficients and standard errors by 35
            lincom ignite_0*70
			
           	local lintemp1 = r(estimate)
			local lin1: display %9.2f `lintemp1'
			
			local linsetemp1 = r(se)
			local linse1: display %9.2f `linsetemp1'
			
			local lin_p1  =r(p)
			
			
			if `lin_p1'<0.01 {
				local star11="***"
			}
			if `lin_p1'>=0.01 & `lin_p1'<0.05 {
				local star11="**"
			}
			if `lin_p1'>=0.05 & `lin_p1'<0.1 {
				local star11="*"
			}
			if `lin_p1'>=0.1 {
				local star11=""
			}
			
			local linc1 = "`lin1'" + "`star11'"
			estadd local combined1 "`linc1'": tsls`v'1any
			local lincse1 = subinstr("(`linse1')"," ","",.)
			estadd local combined_se1 "`lincse1'": tsls`v'1any
        }

*************************************************************
* Make Table
*************************************************************

estout tslsback_jail_3m1any tslsback_jail_6m1any tslsback_jail_9m1any tslsback_jail_12m1any   /// 
    using "$tables/2sls_days_in_jail_main_r4_com6.tex", style(tex)  /// 
    label nobaselevels collabels(none) mlabels(none) /// 
drop($auxiliary_control ignite_0 stay D_ignite_proxy) ///
    cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
    starlevels(* 0.10 ** 0.05 *** 0.01) /// 
    stats(combined1 combined_se1 foo meancont N, /// 
    fmt(%9.3f %9.3f 0 %9.3f %9.0gc) /// 
    labels("Months in IGNITE" " " "\hspace{0.5cm}" "Control Complier Mean" /// 
        "Observations")) replace
		
		